﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleProcessManager : MonoBehaviour
{
	protected List<BPTrigger> lstTrigger;
	protected List<BPInstructionSet> lstInstructionSet;
	protected BattleLevelEntity CurrentLevelEntity { get; set; }
	protected bool IsInit { get;  set; } = false;

	virtual public void Init(BattleLevelEntity levelEntity)
	{
		CurrentLevelEntity = levelEntity;
		lstTrigger = new List<BPTrigger>();
		lstInstructionSet = new List<BPInstructionSet>();

		lstTrigger.AddRange(CurrentLevelEntity.LstTrigger);
		lstInstructionSet.AddRange(CurrentLevelEntity.LstInstructionSet);

		for (int index = 0; index < lstTrigger.Count; index++)
		{
			lstTrigger[index].Init(this, lstInstructionSet[index]);
		}
		IsInit = true;

		#region FAKER INIT
		/*
		for (int i = 0; i < 3; i++)
		{
			var t = new BPTrigger();
			lstTrigger.Add(t);

			//Create Set
			var s = new BPInstructionSet();
			lstInstructionSet.Add(s);

			t.Init(this, s);
		}
		lstTrigger[0].Scene = (int)Scene.BattleScene;
		lstTrigger[0].SceneEvent = (int)SceneEvent.EVENT_SCENE_START;


		lstTrigger[1].Scene = (int)Scene.BattleScene;
		lstTrigger[1].SceneEvent = (int)SceneEvent.EVENT_SCENE_END;

		lstTrigger[2].Scene = (int)Scene.BattleScene;
		lstTrigger[2].SceneEvent = (int)SceneEvent.EVENT_SCENE_END;
		*/
		#endregion
	}

	virtual public void DoUpdate(float deltaTime)
	{
		for (int index = 0; index < lstTrigger.Count; index++)
		{
			//Debug.Log($"lstTrigger {index} Update : {lstTrigger[index].Scene}  {lstTrigger[index].SceneEvent} {lstTrigger[index].IsTriggered}");
			lstTrigger[index].DoUpdate(deltaTime);
		}
	}

	virtual public void Reset()
	{
		IsInit = false;
		//Close Trigger Listening to Event And ShutDown All Instruction

		//TODO To ResetProcess
		IsInit = true;
	}

	virtual public void ReceiveSceneEvent(int ev, int evp)
	{
		if (IsInit == false)
			return;
		for (int index = 0; index < lstTrigger.Count; index++)
		{
			var tempT = lstTrigger[index];
			if (!tempT.IsTriggered)
				tempT.ReceiveSceneEvent(ev, evp);
		}
	}

	virtual public void DisableTrigger(string name)
	{
		var trigger = FindTrigger(name);
		trigger?.Disable();
	}

	virtual public void EnableTrigger(string name)
	{
		var trigger = FindTrigger(name);
		trigger?.Enable();
	}

	virtual public BPTrigger FindTrigger(string name)
	{
		for (int index = 0; index < lstTrigger.Count; index++)
		{
			if (lstTrigger[index].TriggerName == name)
				return lstTrigger[index];
		}
		return null;
	}
}
